package com.yhw.srb.core.controller.admin;

import com.yhw.common.exception.Assert;
import com.yhw.common.exception.BusinessException;
import com.yhw.common.result.R;
import com.yhw.common.result.ResponseEnum;
import com.yhw.srb.core.pojo.entity.IntegralGrade;
import com.yhw.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin  //跨域      //  为什么注释掉？ 和gateway中的跨域配置冲突？ 不要网关、服务端都同时进行跨域配置，否则有问题。
@Slf4j                     //  如果项目用到网关，建议网关中统一跨域配置，这样服务端就可以只专注于业务的处理
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll(){

//        log.info("this is info");
//        log.warn("this is warn");
//        log.error("this is error");

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list);

    }

    @ApiOperation(value = "根据id删除积分等级",notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "数据ID",required = true,example = "100") @PathVariable("id") Long id){

        boolean result = integralGradeService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }

    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){

//        //如果 借款额度为空 就手动抛出一个 自定义异常 ！
//        if(integralGrade.getBorrowAmount() == null){
//            //BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }

        //用 断言Assert 替换 if else
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = integralGradeService.save(integralGrade);
        if (result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }

    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id",required = true,example = "1") @PathVariable("id") Long id){

        IntegralGrade integralGrade = integralGradeService.getById(id);

        if (integralGrade != null){
            return R.ok().data("record",integralGrade);
        }else {
            return R.error().message("数据不存在");
        }

    }

    @ApiOperation("更新积分等级")
    @PostMapping("/update")
    public R updateById(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){

        boolean result = integralGradeService.updateById(integralGrade);
        if (result){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }

    }


}
